package com.raining.inningz.database.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.raining.inningz.database.dao.BattingSummary;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table BATTING_SUMMARY.
*/
public class BattingSummaryDao extends AbstractDao<BattingSummary, Long> {

    public static final String TABLENAME = "BATTING_SUMMARY";

    /**
     * Properties of entity BattingSummary.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property BallNumOnDismissal = new Property(1, Long.class, "ballNumOnDismissal", false, "BALL_NUM_ON_DISMISSAL");
        public final static Property BallsFaced = new Property(2, Long.class, "ballsFaced", false, "BALLS_FACED");
        public final static Property BatsmanNum = new Property(3, Long.class, "batsmanNum", false, "BATSMAN_NUM");
        public final static Property BowlerNum = new Property(4, Long.class, "bowlerNum", false, "BOWLER_NUM");
        public final static Property Dots = new Property(5, Long.class, "dots", false, "DOTS");
        public final static Property FielderNum3 = new Property(6, Long.class, "fielderNum3", false, "FIELDER_NUM3");
        public final static Property FielderNum = new Property(7, Long.class, "fielderNum", false, "FIELDER_NUM");
        public final static Property FielderNum2 = new Property(8, Long.class, "fielderNum2", false, "FIELDER_NUM2");
        public final static Property Fives = new Property(9, Long.class, "fives", false, "FIVES");
        public final static Property Fours = new Property(10, Long.class, "fours", false, "FOURS");
        public final static Property Ones = new Property(11, Long.class, "ones", false, "ONES");
        public final static Property Others = new Property(12, Long.class, "others", false, "OTHERS");
        public final static Property Position = new Property(13, Long.class, "position", false, "POSITION");
        public final static Property RunsScored = new Property(14, Long.class, "runsScored", false, "RUNS_SCORED");
        public final static Property ScoreLineHistorysw = new Property(15, Long.class, "scoreLineHistorysw", false, "SCORE_LINE_HISTORYSW");
        public final static Property Sixes = new Property(16, Long.class, "sixes", false, "SIXES");
        public final static Property Threes = new Property(17, Long.class, "threes", false, "THREES");
        public final static Property TotalOnDismissal = new Property(18, Long.class, "totalOnDismissal", false, "TOTAL_ON_DISMISSAL");
        public final static Property Twos = new Property(19, Long.class, "twos", false, "TWOS");
        public final static Property Undef1 = new Property(20, Long.class, "undef1", false, "UNDEF1");
        public final static Property Uploaddsw = new Property(21, Long.class, "uploaddsw", false, "UPLOADDSW");
        public final static Property WicketsOnDismissal = new Property(22, Long.class, "wicketsOnDismissal", false, "WICKETS_ON_DISMISSAL");
        public final static Property BattingHistoryScoreline = new Property(23, Long.class, "battingHistoryScoreline", false, "BATTING_HISTORY_SCORELINE");
        public final static Property Inning = new Property(24, Long.class, "inning", false, "INNING");
        public final static Property OversOnDismissal = new Property(25, Long.class, "oversOnDismissal", false, "OVERS_ON_DISMISSAL");
        public final static Property Dismissalcd = new Property(26, Long.class, "dismissalcd", false, "DISMISSALCD");
        public final static Property Undef2 = new Property(27, Long.class, "undef2", false, "UNDEF2");
        public final static Property Undef3 = new Property(28, Long.class, "undef3", false, "UNDEF3");
        public final static Property Undef4 = new Property(29, Long.class, "undef4", false, "UNDEF4");
        public final static Property MatchInningBattingSummaryId = new Property(30, Long.class, "matchInningBattingSummaryId", false, "MATCH_INNING_BATTING_SUMMARY_ID");
    };

    private DaoSession daoSession;

    private Query<BattingSummary> matchInnings_BattingSummaryListQuery;

    public BattingSummaryDao(DaoConfig config) {
        super(config);
    }
    
    public BattingSummaryDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'BATTING_SUMMARY' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'BALL_NUM_ON_DISMISSAL' INTEGER," + // 1: ballNumOnDismissal
                "'BALLS_FACED' INTEGER," + // 2: ballsFaced
                "'BATSMAN_NUM' INTEGER," + // 3: batsmanNum
                "'BOWLER_NUM' INTEGER," + // 4: bowlerNum
                "'DOTS' INTEGER," + // 5: dots
                "'FIELDER_NUM3' INTEGER," + // 6: fielderNum3
                "'FIELDER_NUM' INTEGER," + // 7: fielderNum
                "'FIELDER_NUM2' INTEGER," + // 8: fielderNum2
                "'FIVES' INTEGER," + // 9: fives
                "'FOURS' INTEGER," + // 10: fours
                "'ONES' INTEGER," + // 11: ones
                "'OTHERS' INTEGER," + // 12: others
                "'POSITION' INTEGER," + // 13: position
                "'RUNS_SCORED' INTEGER," + // 14: runsScored
                "'SCORE_LINE_HISTORYSW' INTEGER," + // 15: scoreLineHistorysw
                "'SIXES' INTEGER," + // 16: sixes
                "'THREES' INTEGER," + // 17: threes
                "'TOTAL_ON_DISMISSAL' INTEGER," + // 18: totalOnDismissal
                "'TWOS' INTEGER," + // 19: twos
                "'UNDEF1' INTEGER," + // 20: undef1
                "'UPLOADDSW' INTEGER," + // 21: uploaddsw
                "'WICKETS_ON_DISMISSAL' INTEGER," + // 22: wicketsOnDismissal
                "'BATTING_HISTORY_SCORELINE' INTEGER," + // 23: battingHistoryScoreline
                "'INNING' INTEGER," + // 24: inning
                "'OVERS_ON_DISMISSAL' INTEGER," + // 25: oversOnDismissal
                "'DISMISSALCD' INTEGER," + // 26: dismissalcd
                "'UNDEF2' INTEGER," + // 27: undef2
                "'UNDEF3' INTEGER," + // 28: undef3
                "'UNDEF4' INTEGER," + // 29: undef4
                "'MATCH_INNING_BATTING_SUMMARY_ID' INTEGER);"); // 30: matchInningBattingSummaryId
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'BATTING_SUMMARY'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, BattingSummary entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long ballNumOnDismissal = entity.getBallNumOnDismissal();
        if (ballNumOnDismissal != null) {
            stmt.bindLong(2, ballNumOnDismissal);
        }
 
        Long ballsFaced = entity.getBallsFaced();
        if (ballsFaced != null) {
            stmt.bindLong(3, ballsFaced);
        }
 
        Long batsmanNum = entity.getBatsmanNum();
        if (batsmanNum != null) {
            stmt.bindLong(4, batsmanNum);
        }
 
        Long bowlerNum = entity.getBowlerNum();
        if (bowlerNum != null) {
            stmt.bindLong(5, bowlerNum);
        }
 
        Long dots = entity.getDots();
        if (dots != null) {
            stmt.bindLong(6, dots);
        }
 
        Long fielderNum3 = entity.getFielderNum3();
        if (fielderNum3 != null) {
            stmt.bindLong(7, fielderNum3);
        }
 
        Long fielderNum = entity.getFielderNum();
        if (fielderNum != null) {
            stmt.bindLong(8, fielderNum);
        }
 
        Long fielderNum2 = entity.getFielderNum2();
        if (fielderNum2 != null) {
            stmt.bindLong(9, fielderNum2);
        }
 
        Long fives = entity.getFives();
        if (fives != null) {
            stmt.bindLong(10, fives);
        }
 
        Long fours = entity.getFours();
        if (fours != null) {
            stmt.bindLong(11, fours);
        }
 
        Long ones = entity.getOnes();
        if (ones != null) {
            stmt.bindLong(12, ones);
        }
 
        Long others = entity.getOthers();
        if (others != null) {
            stmt.bindLong(13, others);
        }
 
        Long position = entity.getPosition();
        if (position != null) {
            stmt.bindLong(14, position);
        }
 
        Long runsScored = entity.getRunsScored();
        if (runsScored != null) {
            stmt.bindLong(15, runsScored);
        }
 
        Long scoreLineHistorysw = entity.getScoreLineHistorysw();
        if (scoreLineHistorysw != null) {
            stmt.bindLong(16, scoreLineHistorysw);
        }
 
        Long sixes = entity.getSixes();
        if (sixes != null) {
            stmt.bindLong(17, sixes);
        }
 
        Long threes = entity.getThrees();
        if (threes != null) {
            stmt.bindLong(18, threes);
        }
 
        Long totalOnDismissal = entity.getTotalOnDismissal();
        if (totalOnDismissal != null) {
            stmt.bindLong(19, totalOnDismissal);
        }
 
        Long twos = entity.getTwos();
        if (twos != null) {
            stmt.bindLong(20, twos);
        }
 
        Long undef1 = entity.getUndef1();
        if (undef1 != null) {
            stmt.bindLong(21, undef1);
        }
 
        Long uploaddsw = entity.getUploaddsw();
        if (uploaddsw != null) {
            stmt.bindLong(22, uploaddsw);
        }
 
        Long wicketsOnDismissal = entity.getWicketsOnDismissal();
        if (wicketsOnDismissal != null) {
            stmt.bindLong(23, wicketsOnDismissal);
        }
 
        Long battingHistoryScoreline = entity.getBattingHistoryScoreline();
        if (battingHistoryScoreline != null) {
            stmt.bindLong(24, battingHistoryScoreline);
        }
 
        Long inning = entity.getInning();
        if (inning != null) {
            stmt.bindLong(25, inning);
        }
 
        Long oversOnDismissal = entity.getOversOnDismissal();
        if (oversOnDismissal != null) {
            stmt.bindLong(26, oversOnDismissal);
        }
 
        Long dismissalcd = entity.getDismissalcd();
        if (dismissalcd != null) {
            stmt.bindLong(27, dismissalcd);
        }
 
        Long undef2 = entity.getUndef2();
        if (undef2 != null) {
            stmt.bindLong(28, undef2);
        }
 
        Long undef3 = entity.getUndef3();
        if (undef3 != null) {
            stmt.bindLong(29, undef3);
        }
 
        Long undef4 = entity.getUndef4();
        if (undef4 != null) {
            stmt.bindLong(30, undef4);
        }
    }

    @Override
    protected void attachEntity(BattingSummary entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public BattingSummary readEntity(Cursor cursor, int offset) {
        BattingSummary entity = new BattingSummary( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // ballNumOnDismissal
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // ballsFaced
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // batsmanNum
            cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4), // bowlerNum
            cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5), // dots
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // fielderNum3
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7), // fielderNum
            cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8), // fielderNum2
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9), // fives
            cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10), // fours
            cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11), // ones
            cursor.isNull(offset + 12) ? null : cursor.getLong(offset + 12), // others
            cursor.isNull(offset + 13) ? null : cursor.getLong(offset + 13), // position
            cursor.isNull(offset + 14) ? null : cursor.getLong(offset + 14), // runsScored
            cursor.isNull(offset + 15) ? null : cursor.getLong(offset + 15), // scoreLineHistorysw
            cursor.isNull(offset + 16) ? null : cursor.getLong(offset + 16), // sixes
            cursor.isNull(offset + 17) ? null : cursor.getLong(offset + 17), // threes
            cursor.isNull(offset + 18) ? null : cursor.getLong(offset + 18), // totalOnDismissal
            cursor.isNull(offset + 19) ? null : cursor.getLong(offset + 19), // twos
            cursor.isNull(offset + 20) ? null : cursor.getLong(offset + 20), // undef1
            cursor.isNull(offset + 21) ? null : cursor.getLong(offset + 21), // uploaddsw
            cursor.isNull(offset + 22) ? null : cursor.getLong(offset + 22), // wicketsOnDismissal
            cursor.isNull(offset + 23) ? null : cursor.getLong(offset + 23), // battingHistoryScoreline
            cursor.isNull(offset + 24) ? null : cursor.getLong(offset + 24), // inning
            cursor.isNull(offset + 25) ? null : cursor.getLong(offset + 25), // oversOnDismissal
            cursor.isNull(offset + 26) ? null : cursor.getLong(offset + 26), // dismissalcd
            cursor.isNull(offset + 27) ? null : cursor.getLong(offset + 27), // undef2
            cursor.isNull(offset + 28) ? null : cursor.getLong(offset + 28), // undef3
            cursor.isNull(offset + 29) ? null : cursor.getLong(offset + 29) // undef4
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, BattingSummary entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setBallNumOnDismissal(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setBallsFaced(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setBatsmanNum(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setBowlerNum(cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4));
        entity.setDots(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5));
        entity.setFielderNum3(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setFielderNum(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
        entity.setFielderNum2(cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8));
        entity.setFives(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
        entity.setFours(cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10));
        entity.setOnes(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
        entity.setOthers(cursor.isNull(offset + 12) ? null : cursor.getLong(offset + 12));
        entity.setPosition(cursor.isNull(offset + 13) ? null : cursor.getLong(offset + 13));
        entity.setRunsScored(cursor.isNull(offset + 14) ? null : cursor.getLong(offset + 14));
        entity.setScoreLineHistorysw(cursor.isNull(offset + 15) ? null : cursor.getLong(offset + 15));
        entity.setSixes(cursor.isNull(offset + 16) ? null : cursor.getLong(offset + 16));
        entity.setThrees(cursor.isNull(offset + 17) ? null : cursor.getLong(offset + 17));
        entity.setTotalOnDismissal(cursor.isNull(offset + 18) ? null : cursor.getLong(offset + 18));
        entity.setTwos(cursor.isNull(offset + 19) ? null : cursor.getLong(offset + 19));
        entity.setUndef1(cursor.isNull(offset + 20) ? null : cursor.getLong(offset + 20));
        entity.setUploaddsw(cursor.isNull(offset + 21) ? null : cursor.getLong(offset + 21));
        entity.setWicketsOnDismissal(cursor.isNull(offset + 22) ? null : cursor.getLong(offset + 22));
        entity.setBattingHistoryScoreline(cursor.isNull(offset + 23) ? null : cursor.getLong(offset + 23));
        entity.setInning(cursor.isNull(offset + 24) ? null : cursor.getLong(offset + 24));
        entity.setOversOnDismissal(cursor.isNull(offset + 25) ? null : cursor.getLong(offset + 25));
        entity.setDismissalcd(cursor.isNull(offset + 26) ? null : cursor.getLong(offset + 26));
        entity.setUndef2(cursor.isNull(offset + 27) ? null : cursor.getLong(offset + 27));
        entity.setUndef3(cursor.isNull(offset + 28) ? null : cursor.getLong(offset + 28));
        entity.setUndef4(cursor.isNull(offset + 29) ? null : cursor.getLong(offset + 29));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(BattingSummary entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(BattingSummary entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "battingSummaryList" to-many relationship of MatchInnings. */
    public List<BattingSummary> _queryMatchInnings_BattingSummaryList(Long matchInningBattingSummaryId) {
        synchronized (this) {
            if (matchInnings_BattingSummaryListQuery == null) {
                QueryBuilder<BattingSummary> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.MatchInningBattingSummaryId.eq(null));
                matchInnings_BattingSummaryListQuery = queryBuilder.build();
            }
        }
        Query<BattingSummary> query = matchInnings_BattingSummaryListQuery.forCurrentThread();
        query.setParameter(0, matchInningBattingSummaryId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getPlayerDao().getAllColumns());
            builder.append(" FROM BATTING_SUMMARY T");
            builder.append(" LEFT JOIN PLAYER T0 ON T.'BATSMAN_NUM'=T0.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected BattingSummary loadCurrentDeep(Cursor cursor, boolean lock) {
        BattingSummary entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Player player = loadCurrentOther(daoSession.getPlayerDao(), cursor, offset);
        entity.setPlayer(player);

        return entity;    
    }

    public BattingSummary loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<BattingSummary> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<BattingSummary> list = new ArrayList<BattingSummary>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<BattingSummary> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<BattingSummary> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
